package com.dlc.felear.xiaoensale.deviceControlSf;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import java.util.concurrent.ThreadPoolExecutor;

public class JiaobanControl implements Handler.Callback {
    private static final String TAG = "JiaobanControl";
    private static final int WHAT_ZF = 11221;
    private int mNum = 1;
    private ThreadPoolExecutor scheduledThreadPool;
    private int dir = 2;
    private int levelSpeed = 10;
    private MotorDiverControl motorDiverControl;
    private boolean isRun = false;
    private Handler handler;
    private String type = "M400";
    private SensorStatusInfo sensorStatusInfo;
    private int divTime=30;

    public void setSensorStatusInfo(SensorStatusInfo sensorStatusInfo) {
        this.sensorStatusInfo = sensorStatusInfo;
    }

    public JiaobanControl(String serialNum, ThreadPoolExecutor scheduledThreadPool) {
        this.scheduledThreadPool = scheduledThreadPool;
        motorDiverControl = new MotorDiverControl(mNum, serialNum);
        motorDiverControl.setSpeed(levelSpeed);
        handler = new Handler(this);
    }

    public void setType(String type) {
        this.type = type;
        log("type=" + type);
    }

    /**
     * 搅拌动作
     *
     * @param value
     */
    public void jiaobanAction(int dir, int value) {
        log("接收搅拌指令:" + value + " dir:" + dir);
        value = dir > value ? dir : value;
        if (value <= 0) {
            stopJiaoban();
            return;
        }
//        if (value > 9) {
//            value = 6;
//        }
        this.dir=(dir==1?1:2);
        startJiaoban(getLevel(value));
    }

    /**
     * 洗锅搅拌
     *
     * @param delay
     */
    public void xgJiaoban(final int delay) {
        log("洗锅搅拌");
        scheduledThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    startJiaoban(5);
                    Thread.sleep(delay * 1000);
                    stopJiaoban();
                } catch (Exception e) {

                }
            }
        });
    }

    /**
     * 菜出锅转动
     */
    public void cgJiaoban() {
        log("出菜搅拌");
        scheduledThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    startJiaoban(getLevel(1));
                } catch (Exception e) {

                }
            }
        });
    }

    public void initStartSpeed() {
        if (!isRun) {
            startJiaoban(getLevel(6));
        }
    }

    /**
     * 停止搅拌
     */
    public void stopJiaoban() {
        log("停止转动");
        isRun = false;
        handler.removeMessages(WHAT_ZF);
        motorDiverControl.stop(0);
    }

    /**
     * 档位（1-17）
     *
     * @param level
     */
    private void startJiaoban(final int level) {
        log("开始搅拌:搅拌速度：" + level);
        scheduledThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    isRun = true;
                    setSpeed(level);
                    alwaysRun(dir);
                } catch (Exception e) {

                }
            }
        });
    }

    /**
     * @param dir
     */
    private void alwaysRun(int dir) {
        log("一直转：dir=" + dir);
        motorDiverControl.setAlwaysRun(dir);
    }

    /**
     * 设置速度
     *
     * @param level
     */
    private void setSpeed(int level) {
        log("设置速度:" + level);
        motorDiverControl.setSpeed(level);
    }

    private int getLevel(int level) {
//        return level;
        return getM400Level(level);
    }


    private int getM400Level(int level) {
        int temp = 25;
        if (level < 1 || level > 9) {
            return temp;
        }
        if (level == 1) {
            temp = 11;
        }
        if (level == 2) {
            temp = 15;
        }
        if (level == 3) {
            temp = 25;
        }
        if (level == 4) {
            temp = 30;
        }
        if (level == 5) {
            temp = 35;
        }
        if (level == 6) {
            temp = 40;
        }
        if (level == 7) {
            temp = 43;
        }
        if (level == 8) {
            temp = 46;
        }
        if (level == 9) {
            temp = 50;
        }
        return temp;
    }


    /**
     * @param flag f炒菜结束 t炒菜开始
     */
    public void isChaocaiStatus(boolean flag) {

    }


    private void log(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            Log.e(TAG, msg);
        }
    }

    public void allStop() {
        motorDiverControl.stop(0);
    }

    @Override
    public boolean handleMessage(Message msg) {
        changeDir();
        return false;
    }

    private void changeDir() {
        try {
            if (sensorStatusInfo.getMotorDir() == 1) {
                alwaysRun(2);
            } else {
                alwaysRun(1);
            }
        } catch (Exception e) {
            log("handleMessage" + e.getMessage());
        }
    }

    /**
     * 正反转切换 切换3次
     *
     * @param sec
     */
    public void controlZfRun(int sec) {
        try {
//            changeDir();
//            int temp = ((sec + divTime) / divTime);
//            changeDir();
//            log("切换正反转:msec=" + sec + " 反转：" + temp);
//            for (int i = 0; i < 1+temp; i++) {
//                handler.sendEmptyMessageDelayed(WHAT_ZF, i*temp*1000);
//            }
        } catch (Exception e) {
            log("切换正反转:msec=" + sec + " 反转：" + e.getMessage());
        }
    }
}
